<template>
	<view>
		<uni-nav-bar left-icon="left" left-text="返回" @clickLeft="gos" height="140rpx"  title="我的车辆" right-text="添加车辆" @clickRight="addcar"/>
		<view class="">
			<image src="../../../../static/images/index/che.png" mode=""></image>
		</view>
		<checkbox-group @change="checkboxChange">
			<label class="uni-list-cell uni-list-cell-pd" v-for="item in list" :key="item.id">
				<view class="label">
					<view class="bust">
						<view class="">
							<image src="../../../../static/images/index/我的车辆-2_03.jpg" mode="" class="image"></image>
						</view>
						<view>{{item.carno}}</view>
					</view>
					<view>
						<checkbox :value="item.id+''" :checked="item.checked" />
					</view>
				</view>
			</label>
		</checkbox-group>
		<button @tap="delCar">删除</button>
	</view>
</template>
<script>
	import Index from '@/apis/index.js'
	export default {
		data() {
			return {
				list:[],
				ids:[]
			};
		},
		onLoad() {
			this.getCarList()
		},
		methods:{
			gos(){
				uni.switchTab({
					url:'/pages/My/My'
				})
			},
			checkboxChange(e){
				console.log(e)
				this.ids = e.detail.value
			},
			addcar(){
				uni.navigateTo({
					url:'/pages/My/Vehicle/Vehicle'
				})
			},
			
			async getCarList(){
				let res = await Index.usercarlist()
				console.log(res);
				this.list = res.data.list.map(item=>{
					return{
						...item,
						checked:false
					}
				})
			},
			delCar(){
				//1.拼接数据
				let data = {
					ids:this.ids
				}
				//2.如果一辆车都没有选，提醒一下
				if(this.ids.length === 0){
					uni.showToast({
						title:"选择车辆",
						icon:'error'
					})
				}
				
				//3.调接口
				uni.showModal({
					title: '提示',
						content: '您确定要删除吗',
						success: async (res)=> {
							if (res.confirm) {
								let res = await Index.usercardeleteall(data);
								if(res.errCode === 10000){
									uni.showToast({
										title:'删除成功',
										icon:'success'
									})
									this.getCarList()
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
				})
			},
		}
	}
</script>

<style lang="less">
	@import url('@/static/css/my/vehicle/AddVehicle/AddVehicle.css');
</style>
